package com.link.base.base.product.controller;

import com.link.base.base.product.model.Price;
import com.link.base.base.product.service.PriceService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.query.QueryParams;
import com.link.core.util.AppConstants;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 价格处理
 */
@Controller
@RequestMapping("/link/price")
public class PriceController {

    @Resource
    private PriceService priceService;

    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(QueryParams qps, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        qps.setAttr1(AppConstants.MARKET_ORG);
        qps.setAttr2(AppConstants.DIS_CHANNEL);
        try {
            List<Price> list = priceService.queryPriceListPage(qps);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", qps.getTotal());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add(@JsonParam Price record, @JsonParam QueryParams qps,
                                   HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            priceService.addPriceInsert(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(@JsonParam Price record, @JsonParam QueryParams qps,
                                      HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            priceService.priceUpdate(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> del(@JsonParam Price record, @JsonParam QueryParams qps,
                                   HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            priceService.delPriceById(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
